//
//  ArticleDetailCell.swift
//  LearnDemo
//
//  Created by 振轩 on 2018/4/9.
//  Copyright © 2018年 振轩. All rights reserved.
//

//do{
//    let attrStr = try NSAttributedString(data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
//
//    msgLabel.attributedText = attrStr
//}catch let error as NSError {
//    print(error.localizedDescription)
//}



import UIKit

class ArticleDetailCell: UITableViewCell {
    
    var  reloadCallBack:(_ height:CGFloat)->() = {_ in
        
    }
    var cellHeight:Float = 0
    var rowHeight:CGFloat = 0
    var mode:ArticleDetail?{
        
        didSet{
            
            
            sourceLabel.text = "发布于:" + mode!.addDate!
            
         
            if mode?.isLike == 0 {
                likeBTN.setImage(UIImage.init(named: "dz"), for: .normal)
            }else{
                likeBTN.setImage(UIImage.init(named: "dz2"), for: .normal)
            }
            collectCountLabel.text = "\(String(describing: mode!.collectNum))"
            likeCountLabel.text = "\(String(describing: mode!.likeNum))"
            if mode?.isCollect == 0 {
                collectBTN.setImage(UIImage.init(named: "sc2"), for: .normal)
            }else{
                collectBTN.setImage(UIImage.init(named: "sc"), for: .normal)
                
            }
            
            
            
            
            
            //ArtcleImageView.loadWebImage(withName: mode?.thumb, andIsOrgin: true)
            
            TitleLb.text = mode?.title
            viewCountLabel.text = "\(String(describing: mode!.viewNum))" + "次浏览"
            let content:NSString =  mode?.content! as! NSString
             let contentStr = NSString.localizedStringWithFormat("<head><style>img{width:%f !important;height:auto}</style></head>%@", kScreenW,content)
            //webview.loadHTMLString((mode?.content)!, baseURL: nil)
            webview.loadHTMLString(contentStr as String, baseURL: nil)
        }
        
    }
    @IBOutlet weak var titleLabel: UIView!
    
    @IBOutlet weak var sourceLabel: UILabel!
    
    @IBOutlet weak var viewCountLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    
    
    @IBOutlet weak var ArtcleImageView: UIImageView!
    
    @IBOutlet weak var likeCountLabel: UILabel!
    
    @IBOutlet weak var collectCountLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    
    @IBOutlet weak var collectBTN: UIButton!
    
    @IBOutlet weak var likeBTN: UIButton!
    @IBOutlet weak var webview: UIWebView!
    
    @IBOutlet weak var TitleLb: UILabel!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        webview.delegate = self
        webview.scrollView.bounces = false
        webview.scrollView.showsHorizontalScrollIndicator = false
        webview.scrollView.showsVerticalScrollIndicator = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    @IBAction func collectTap(_ sender: Any) {
        loadRefresh(url: "/index.php/FoodCheckApi/courseCollect.json",isLike: false)
        
        
        
    }
    @IBAction func LickClick(_ sender: Any) {
         loadRefresh(url: "/index.php/FoodCheckApi/courseLike.json", isLike: true)
        
    }
    
    
    func loadRefresh(url:String,isLike:Bool){
        
        
        var params = [String:AnyObject]()
        params["courseId"] = mode?.id as AnyObject
        
        params["staffId"] = LoginStatuesModel.getStaffId() as AnyObject
        params["staffType"] = LoginStatuesModel.getUserType() as AnyObject
        let API:BaseReqApi = BaseReqApi.init(requestUrl: url, andrequestTime: 5, andParams: params as! [AnyHashable : Any], andRequestMethod: YTKRequestMethod.POST, andCache: false, andCacheTime: 0, andPostToken: true)
        API.starRequest { (status, mess, response) in
            
            guard let dic:Dictionary = response as? [String : AnyObject] else{
                
                return
            }
            print("_______***______")
            print(dic)
            print("_______***______")
            let Dir:[String:NSObject] = dic["data"] as! [String : NSObject]
            //let mode1  = CourseDetail.init(DIC: dic)
            if isLike{
                
                
                
                let mode1 :ArticleDetail = self.mode!
                
                mode1.isLike = Dir["isLike"] as! NSInteger
                mode1.likeNum = Dir["total"] as! NSInteger
                self.mode = mode1
                
                
            }else{
                
                let mode1 :ArticleDetail = self.mode!
                
                mode1.isCollect = Dir["isCollect"] as! NSInteger
                mode1.collectNum = Dir["total"] as! NSInteger
                
                self.mode = mode1
                
            }
            
        }
    }
    deinit {
        self.webview.delegate = nil
        
        self.webview.stopLoading()
    }
    
}


extension ArticleDetailCell:UIWebViewDelegate{
    func webViewDidFinishLoad(_ webView: UIWebView){
        

        
        var frame:CGRect = webView.frame
        webView.frame.size.width = kScreenW
        frame.size.height = 1
       webView.frame = frame
        let  height = webView.scrollView.contentSize.height
        webView.frame = frame
        print("-----1----")
        print(height)
        print("-----1-----")
        print(webView.frame)
        print(self.webview.frame)
        webView.frame.size.height = height
       
//        layoutIfNeeded()
//        layoutSubviews()
      
        reloadCallBack(CGFloat(height))
        
        }
    
    
}
